﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PMS.Utilities;
using PMS.ENT;
using PMS.ENT.Report;
using PMS.BLL;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Drawing.Printing;
using System.IO;

namespace PMS.Controllers
{
    public class ReportController : Controller
    {
        
        #region Project Report

        public ActionResult ProjectReport()
        {
            LoadDataForProject(0, 0, Utility.semesterID);
            try
            {
                return View();
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ProjectReport(string action, int semesterID, int departmentID, int subjectID)
        {
            try
            {
                ProjectCollection listProject = ProjectBLL.GetProjectList(semesterID, 0, 0);
                if (listProject.Count <= 0)
                {
                    TempData["Message"] = "Attention, No record found!";
                    LoadDataForProject(subjectID, departmentID, semesterID);
                    return View();
                }
                SemesterENT semester = SemesterBLL.GetSemesterByID(semesterID);
                ProjectRPT projectRPT = new ProjectRPT();
                projectRPT.SetDataSource(listProject);
                projectRPT.SetParameterValue("Semester", semester.SemesterName);
                projectRPT.SetParameterValue("FromDate", semester.FromDate);
                projectRPT.SetParameterValue("ToDate", semester.ToDate);

                Stream stream;
                if (action == ActionName.PDF.ToString())
                {
                    stream = projectRPT.ExportToStream(ExportFormatType.PortableDocFormat);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.pdf");
                    Response.AddHeader("content-type", "application/pdf");
                }
                else if (action == ActionName.Word.ToString())
                {
                    stream = projectRPT.ExportToStream(ExportFormatType.WordForWindows);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.doc");
                    Response.AddHeader("content-type", "application/msword");
                }
                else
                {
                    stream = projectRPT.ExportToStream(ExportFormatType.WordForWindows);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.doc");
                    Response.AddHeader("content-type", "application/doc");
                }

                stream.Seek(0, System.IO.SeekOrigin.Begin);

                StreamReader reader = new StreamReader(stream);
                byte[] fileData = new byte[stream.Length];
                stream.Read(fileData, 0, (int)stream.Length);


                Response.AddHeader("Content-Length", stream.Length.ToString());
                Response.BinaryWrite(fileData);
                stream.Close();

                return null;
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        public ActionResult StudentProjectReport()
        {
            try
            {
                LoadDataForProject(0, 0, Utility.semesterID);
                return View();
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult StudentProjectReport(string action, int semesterID, int departmentID, int subjectID)
        {
            try
            {
                StudentProjectCollection listStudentProject = ProjectBLL.GetStudentProjectList(semesterID, departmentID, subjectID);

                if (listStudentProject.Count <= 0)
                {
                    TempData["Message"] = "Attention, No record found!";
                    LoadDataForProject(subjectID, departmentID, semesterID);
                    return View();
                }
                SemesterENT semester = SemesterBLL.GetSemesterByID(semesterID);
                StudentProjectRPT studentProjectRPT = new StudentProjectRPT();
                studentProjectRPT.SetDataSource(listStudentProject);
                studentProjectRPT.SetParameterValue("Semester", semester.SemesterName);
                studentProjectRPT.SetParameterValue("FromDate", semester.FromDate);
                studentProjectRPT.SetParameterValue("ToDate", semester.ToDate);

                Stream stream;
                if (action == ActionName.PDF.ToString())
                {
                    stream = studentProjectRPT.ExportToStream(ExportFormatType.PortableDocFormat);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.pdf");
                    Response.AddHeader("content-type", "application/pdf");
                }
                else if (action == ActionName.Word.ToString())
                {
                    stream = studentProjectRPT.ExportToStream(ExportFormatType.WordForWindows);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.doc");
                    Response.AddHeader("content-type", "application/msword");
                }
                else
                {
                    stream = studentProjectRPT.ExportToStream(ExportFormatType.WordForWindows);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.doc");
                    Response.AddHeader("content-type", "application/msword");
                }

                StreamReader reader = new StreamReader(stream);
                byte[] fileData = new byte[stream.Length];
                stream.Read(fileData, 0, (int)stream.Length);

                Response.AddHeader("Content-Length", stream.Length.ToString());
                Response.BinaryWrite(fileData);
                stream.Close();

                return null;
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }


        public ActionResult StudentReport()
        {
            try
            {
                LoadDataForProject(0, 0, Utility.semesterID);
                return View();
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult StudentReport(string action, int semesterID, int departmentID, int subjectID)
        {
            try
            {
                StudentProjectCollection listStudentProject = ProjectBLL.GetStudentList(semesterID, departmentID, subjectID);
                if (listStudentProject.Count <= 0)
                {
                    TempData["Message"] = "Attention, No record found!";
                    LoadDataForProject(subjectID, departmentID, semesterID);
                    return View();
                }
                SemesterENT semester = SemesterBLL.GetSemesterByID(semesterID);
                StudentRPT studentRPT = new StudentRPT();
                studentRPT.SetDataSource(listStudentProject);
                studentRPT.SetParameterValue("Semester", semester.SemesterName);
                studentRPT.SetParameterValue("FromDate", semester.FromDate);
                studentRPT.SetParameterValue("ToDate", semester.ToDate);

                Stream stream;
                if (action == ActionName.PDF.ToString())
                {
                    stream = studentRPT.ExportToStream(ExportFormatType.PortableDocFormat);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=StudentReport.pdf");
                    Response.AddHeader("content-type", "application/pdf");
                }
                else if (action == ActionName.Word.ToString())
                {
                    stream = studentRPT.ExportToStream(ExportFormatType.WordForWindows);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=StudentReport.doc");
                    Response.AddHeader("content-type", "application/msword");
                }
                else
                {
                    stream = studentRPT.ExportToStream(ExportFormatType.WordForWindows);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=StudentReport.doc");
                    Response.AddHeader("content-type", "application/msword");
                }

                StreamReader reader = new StreamReader(stream);
                byte[] fileData = new byte[stream.Length];
                stream.Read(fileData, 0, (int)stream.Length);

                Response.AddHeader("Content-Length", stream.Length.ToString());
                Response.BinaryWrite(fileData);
                stream.Close();

                return null;
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        #region LoadData
        public void LoadDataForProject(int subjectID, int departmentID, int semesterID)
        {
            ViewData["SubjectList"] = new SelectList(SubjectBLL.GetSubjectListForCMB(departmentID), SubjectColumns.SubjectID.ToString(), SubjectColumns.SubjectName.ToString(), subjectID);
            ViewData["DepartmentList"] = new SelectList(DepartmentBLL.GetDepartmentListForCMB(), DepartmentColumns.DepartmentID.ToString(), DepartmentColumns.DepartmentName.ToString(), departmentID);
            ViewData["SemesterList"] = new SelectList(SemesterBLL.GetSemesterListForCMB(), SemesterColumns.SemesterID.ToString(), SemesterColumns.SemesterName.ToString(), semesterID);

        }
        #endregion
        #endregion

        #region Schedule Report
        public ActionResult ProgressReportPDF(int ID)
        {
            int groupID = 0;
            int projectOwnerID = 0;
            int task = 0;
            decimal percent = 0;
            decimal total = 0;

            ProjectENT project = ProjectBLL.GetProjectByScheduleGroupID(ID);

            ScheduleItemCollection listScheduleItem = ScheduleItemBLL.GetScheduleItemListByScheduleGroupID(ID, out groupID, out projectOwnerID, out task, out percent, out total);

            GroupENT group = GroupBLL.GetGroupByID(groupID);

            ProgressRPT progressRPT = new ProgressRPT();
            progressRPT.SetDataSource(listScheduleItem);

            DateTime fromDate = listScheduleItem.Min(m => m.FromDate);
            DateTime toDate = listScheduleItem.Max(m => m.ToDate);

            progressRPT.SetParameterValue("ProjectName", project.ProjectName);
            progressRPT.SetParameterValue("TeacherName", project.Owner);
            progressRPT.SetParameterValue("GroupName", group.GroupName);
            progressRPT.SetParameterValue("Time", fromDate.ToString("dd/MM/yyyy") + " - " + toDate.ToString("dd/MM/yyyy"));
            progressRPT.SetParameterValue("Mark", total);

            Stream stream = progressRPT.ExportToStream(ExportFormatType.PortableDocFormat);
            stream.Seek(0, System.IO.SeekOrigin.Begin);

            StreamReader reader = new StreamReader(stream);
            byte[] fileData = new byte[stream.Length];
            stream.Read(fileData, 0, (int)stream.Length);

            Response.AddHeader("content-dispostion", "attachemnt; filename=studentreport.pdf");
            Response.AddHeader("content-type", "application/pdf");
            Response.AddHeader("Content-Length", stream.Length.ToString());
            Response.BinaryWrite(fileData);
            stream.Close();

            return null;
        }


        public ActionResult ProgressReportWord(int ID)
        {
            int groupID = 0;
            int projectOwnerID = 0;
            int task = 0;
            decimal percent = 0;
            decimal total = 0;
            ProjectENT project = ProjectBLL.GetProjectByScheduleGroupID(ID);

            ScheduleItemCollection listScheduleItem = ScheduleItemBLL.GetScheduleItemListByScheduleGroupID(ID, out groupID, out projectOwnerID, out task, out percent, out total);

            GroupENT group = GroupBLL.GetGroupByID(groupID);

            ProgressRPT progressRPT = new ProgressRPT();
            progressRPT.SetDataSource(listScheduleItem);

            DateTime fromDate = listScheduleItem.Min(m => m.FromDate);
            DateTime toDate = listScheduleItem.Max(m => m.ToDate);

            progressRPT.SetParameterValue("ProjectName", project.ProjectName);
            progressRPT.SetParameterValue("TeacherName", project.Owner);
            progressRPT.SetParameterValue("GroupName", group.GroupName);
            progressRPT.SetParameterValue("Time", fromDate.ToString("dd/MM/yyyy") + " - " + toDate.ToString("dd/MM/yyyy"));
            progressRPT.SetParameterValue("Mark", total);

            Stream stream = progressRPT.ExportToStream(ExportFormatType.WordForWindows);
            stream.Seek(0, System.IO.SeekOrigin.Begin);

            StreamReader reader = new StreamReader(stream);
            byte[] fileData = new byte[stream.Length];
            stream.Read(fileData, 0, (int)stream.Length);

            Response.AddHeader("content-dispostion", "attachemnt; filename=studentreport.doc");
            Response.AddHeader("content-type", "application/msword");
            Response.AddHeader("Content-Length", stream.Length.ToString());
            Response.BinaryWrite(fileData);
            stream.Close();

            return null;
        }

        public ActionResult ScheduleReport()
        {
            try
            {
                try
                {
                    LoadDataForScheduleReport(0, 0, Utility.semesterID, 0, 0);
                    return View();
                }
                catch (DataAccessException)
                {
                    TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                    return RedirectToAction("Error", "Home");
                }
                catch (Exception ex)
                {
                    LogBLL.LogEx("BLM_ERR_Common", ex);
                    TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                    return RedirectToAction("Error", "Home");
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        [HttpPost]
        public ActionResult ScheduleReport(string action,int projectID,int groupID,int semesterID,int departmentID,int subjectID)
        {

            try
            {
                int task = 0;
                decimal percent = 0;
                decimal total = 0;
                ProjectENT project = ProjectBLL.GetProjectByID(projectID);

                ScheduleItemCollection listScheduleItem = ScheduleItemBLL.GetScheduleItemListForReport(groupID, projectID, semesterID, out task, out percent, out total);
                if (listScheduleItem.Count <= 0)
                {
                    TempData["Message"] = "Attention, No record found!";
                    LoadDataForScheduleReport(projectID,groupID,semesterID,departmentID, subjectID);
                    return View();
                }
                GroupENT group = GroupBLL.GetGroupByID(groupID);

                ProgressRPT progressRPT = new ProgressRPT();
                progressRPT.SetDataSource(listScheduleItem);

                DateTime fromDate = listScheduleItem.Min(m => m.FromDate);
                DateTime toDate = listScheduleItem.Max(m => m.ToDate);

                progressRPT.SetParameterValue("ProjectName", project.ProjectName);
                progressRPT.SetParameterValue("TeacherName", project.Owner);
                progressRPT.SetParameterValue("GroupName", group.GroupName);
                progressRPT.SetParameterValue("Time", fromDate.ToString("dd/MM/yyyy") + " - " + toDate.ToString("dd/MM/yyyy"));
                progressRPT.SetParameterValue("Mark", total);

                Stream stream;
                if (action == ActionName.PDF.ToString())
                {
                    stream = progressRPT.ExportToStream(ExportFormatType.PortableDocFormat);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.pdf");
                    Response.AddHeader("content-type", "application/pdf");
                }
                else if (action == ActionName.Excel.ToString())
                {
                    stream = progressRPT.ExportToStream(ExportFormatType.WordForWindows);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.doc");
                    Response.AddHeader("content-type", "application/msword");
                }
                else
                {
                    stream = progressRPT.ExportToStream(ExportFormatType.WordForWindows);
                    Response.AddHeader("content-dispostion", "attachemnt; filename=Projectreport.doc");
                    Response.AddHeader("content-type", "application/msword");
                }
                stream.Seek(0, System.IO.SeekOrigin.Begin);

                StreamReader reader = new StreamReader(stream);
                byte[] fileData = new byte[stream.Length];
                stream.Read(fileData, 0, (int)stream.Length);

                Response.AddHeader("Content-Length", stream.Length.ToString());
                Response.BinaryWrite(fileData);
                stream.Close();
                LoadDataForScheduleReport(projectID, groupID, semesterID, departmentID, subjectID);
                return null;
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        #region LoadData
        public void LoadDataForScheduleReport(int projectID, int groupID, int semesterID, int departmentID, int subjectID)
        {
            ViewData["SubjectList"] = new SelectList(SubjectBLL.GetSubjectListForCMB(departmentID), SubjectColumns.SubjectID.ToString(), SubjectColumns.SubjectName.ToString(), subjectID);
            ViewData["DepartmentList"] = new SelectList(DepartmentBLL.GetDepartmentListForCMB(), DepartmentColumns.DepartmentID.ToString(), DepartmentColumns.DepartmentName.ToString(), departmentID);
            ViewData["SemesterList"] = new SelectList(SemesterBLL.GetSemesterListForCMB(), SemesterColumns.SemesterID.ToString(), SemesterColumns.SemesterName.ToString(), semesterID);
            ViewData["ProjectList"] = new SelectList(ProjectBLL.GetProjectListForCMB(0), ProjectColumns.ProjectID.ToString(), ProjectColumns.ProjectName.ToString(), projectID);
            ViewData["GroupList"] = new SelectList(GroupBLL.GetGroupListForCMB(), GroupColumns.GroupID.ToString(),GroupColumns.GroupName.ToString(), projectID);
        }
        #endregion
        #endregion
    }
}
